<?php
/*
 * File block.access.php for project Rijlesplanner.nl
 * Created on 28 nov 2007 at 22:38:24 by Ivar Pruijn
 */

/**
 * Determines whether the current user has access to the module specified in the 'module' parameter
 * @tutorial http://smarty.php.net/manual/en/plugins.block.functions.php Smarty Tutorial
 * @param array $params The parameters passed to this function from the Smarty template
 * @param string $content The content between the function blocks
 * @param Smarty $smarty The Smarty template
 * @param boolean $repeat TRUE at the first call of the block-function (the opening tag) and FALSE on all subsequent calls to the block function (the block's closing tag)
 */
function smarty_block_access($params, $content, &$smarty, &$repeat) {
	global $logger;
	if (!isset($params['module']) || empty($params['module'])) {
		   return;
    } else {
    	$module = $params['module'];
    	$user = $_SESSION['user'];

    	if(!array_key_exists($module,$user->getACL())) {
//			$logger->log("Module ".$module." not in ACL",PEAR_LOG_DEBUG);
			return;
    	} else if (isset($params['option']) && !empty($params['option'])) {
   			$options = array();
    		$acl = $user->getACL($module);
    		if(isset($acl['options']) && is_array($acl['options'])) {
    			$options = array_values($acl['options']);
    		}
//    		$logger->log("Options for this module: ".print_r($options,true)."",PEAR_LOG_DEBUG);
//    		$logger->log("Option under evaluation: ".$params['option']."...",PEAR_LOG_DEBUG);
	    	if(!in_array($params['option'],$options)) {
//				$logger->log("Option ".$params['option']." for module ".$module." not in ACL",PEAR_LOG_DEBUG);
				return;
	    	} else {
//    			$logger->log("Option ".$params['option']." authorized, returning content ".$content,PEAR_LOG_DEBUG);
	    	}
    	} else if (isset($params['action']) && !empty($params['action'])) {
   			$actions = array();
    		$acl = $user->getACL($module);
    		if(isset($acl['actions']) && is_array($acl['actions'])) {
    			$actions = array_values($acl['actions']);
    		}
//    		$logger->log("Actions for this module: ".print_r($actions,true)."",PEAR_LOG_DEBUG);
//    		$logger->log("Action under evaluation: ".$params['action']."...",PEAR_LOG_DEBUG);
	    	if(!in_array($params['action'],$actions)) {
//				$logger->log("Action ".$params['action']." for module ".$module." not in ACL",PEAR_LOG_DEBUG);
				return;
	    	} else {
//    			$logger->log("Action ".$params['action']." authorized...",PEAR_LOG_DEBUG);
	    	}
    	}
//		$logger->log("Returning content ".$content,PEAR_LOG_DEBUG);
   		return $content;
    }
}
?>
